package com.xinwangai.jingbai.controller;

import com.alibaba.fastjson.JSONObject;
import com.xinwangai.jingbai.pojo.DTO.DictionaryDTO;
import com.xinwangai.jingbai.pojo.VO.DictionaryVO;
import com.xinwangai.jingbai.pojo.VO.DictionaryTypeVO;
import com.xinwangai.jingbai.service.DictionaryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典控制器
 *
 * @author guo.wenliang
 * @since 2025/1/15
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    /**
     * 获取字典类型列表
     */
    @PostMapping("/getDictionaryTypes")
    public JSONObject getDictionaryTypes() {
        JSONObject jsonObject = new JSONObject();
        List<DictionaryTypeVO> dictTypes = dictionaryService.getDictionaryTypes();
        jsonObject.put("code", "0");
        jsonObject.put("data", dictTypes);
        return jsonObject;
    }

    /**
     * 获取字典数据列表
     */
    @PostMapping("/getDictionary")
    public JSONObject getDictionary(@RequestBody DictionaryDTO dictionaryDTO) {
        JSONObject jsonObject = new JSONObject();
        List<DictionaryVO> dictionaries = dictionaryService.getDictionaryList(dictionaryDTO);
        jsonObject.put("code", "0");
        jsonObject.put("data", dictionaries);
        return jsonObject;
    }

    /**
     * 新增字典数据
     */
    @PostMapping("/addDictionary")
    public JSONObject addDictionary(@RequestBody DictionaryDTO dictionaryDTO) {
        JSONObject jsonObject = new JSONObject();
        boolean success = dictionaryService.addDictionary(dictionaryDTO);
        if (success) {
            jsonObject.put("code", "0");
            jsonObject.put("msg", "新增成功");
        } else {
            jsonObject.put("code", "1");
            jsonObject.put("msg", "新增失败");
        }
        return jsonObject;
    }

    /**
     * 更新字典数据
     */
    @PostMapping("/updateDictionary")
    public JSONObject updateDictionary(@RequestBody DictionaryDTO dictionaryDTO) {
        JSONObject jsonObject = new JSONObject();
        boolean success = dictionaryService.updateDictionary(dictionaryDTO);
        if (success) {
            jsonObject.put("code", "0");
            jsonObject.put("msg", "更新成功");
        } else {
            jsonObject.put("code", "1");
            jsonObject.put("msg", "更新失败");
        }
        return jsonObject;
    }

    /**
     * 停用/启用字典数据
     */
    @PostMapping("/toggleDictionaryStatus")
    public JSONObject toggleDictionaryStatus(@RequestBody DictionaryDTO dictionaryDTO) {
        JSONObject jsonObject = new JSONObject();
        // 切换状态：0变1，1变0
        String newStatus = "0".equals(dictionaryDTO.getStatus()) ? "1" : "0";
        dictionaryDTO.setStatus(newStatus);

        boolean success = dictionaryService.updateDictionary(dictionaryDTO);
        if (success) {
            jsonObject.put("code", "0");
            jsonObject.put("msg", "0".equals(newStatus) ? "启用成功" : "停用成功");
        } else {
            jsonObject.put("code", "1");
            jsonObject.put("msg", "操作失败");
        }
        return jsonObject;
    }

    /**
     * 根据字典类型获取字典项
     */
    @PostMapping("/getDictionaryByType")
    public JSONObject getDictionaryByType(@RequestBody DictionaryDTO dictionaryDTO) {
        JSONObject jsonObject = new JSONObject();
        // 如果没有传递status，则查询所有状态的数据
        String status = dictionaryDTO.getStatus();
        if (status == null) {
            status = ""; // 空字符串表示查询所有状态
        }
        List<DictionaryVO> dictionaries = dictionaryService.getDictionaryByType(
            dictionaryDTO.getDictType(),
            status
        );
        jsonObject.put("code", "0");
        jsonObject.put("data", dictionaries);
        return jsonObject;
    }
}
